/**
 * Settle模块 - 支付管理API
 * @description 支付管理相关的API接口定义
 * @author AI Assistant
 * @since 1.0.0
 */

import request from '@/config/axios'
import type { PaymentOrderVO, PaymentOrderPageReqVO, PaymentRefundVO, PageResult } from './types'

/**
 * 获取支付订单分页列表
 * @param params 分页查询参数
 * @returns 支付订单分页数据
 */
export const getPaymentOrderPageApi = (
  params: PaymentOrderPageReqVO
): Promise<PageResult<PaymentOrderVO>> => {
  return request.get({ url: '/settle/payment-order/page', params })
}

/**
 * 获取支付订单详情
 * @param id 订单ID
 * @returns 支付订单详情
 */
export const getPaymentOrderApi = (id: number): Promise<PaymentOrderVO> => {
  return request.get({ url: `/settle/payment-order/get?id=${id}` })
}

/**
 * 根据订单号获取支付订单详情
 * @param orderNo 订单号
 * @returns 支付订单详情
 */
export const getPaymentOrderByNoApi = (orderNo: string): Promise<PaymentOrderVO> => {
  return request.get({ url: `/settle/payment-order/get-by-no?orderNo=${orderNo}` })
}

/**
 * 手动同步支付状态
 * @param id 订单ID
 * @returns 同步结果
 */
export const syncPaymentStatusApi = (id: number): Promise<boolean> => {
  return request.post({ url: `/settle/payment-order/sync-status?id=${id}` })
}

/**
 * 关闭支付订单
 * @param id 订单ID
 * @param remark 关闭原因
 * @returns 关闭结果
 */
export const closePaymentOrderApi = (id: number, remark?: string): Promise<boolean> => {
  return request.put({
    url: '/settle/payment-order/close',
    data: { id, remark }
  })
}

/**
 * 获取退款记录分页列表
 * @param params 分页查询参数
 * @returns 退款记录分页数据
 */
export const getPaymentRefundPageApi = (params: {
  pageNo: number
  pageSize: number
  refundNo?: string
  originalOrderNo?: string
  merchantNo?: string
  refundStatus?: string
  createTime?: [string, string]
}): Promise<PageResult<PaymentRefundVO>> => {
  return request.get({ url: '/settle/payment-refund/page', params })
}

/**
 * 获取退款记录详情
 * @param id 退款ID
 * @returns 退款记录详情
 */
export const getPaymentRefundApi = (id: number): Promise<PaymentRefundVO> => {
  return request.get({ url: `/settle/payment-refund/get?id=${id}` })
}

/**
 * 创建退款申请
 * @param data 退款申请数据
 * @returns 创建结果
 */
export const createPaymentRefundApi = (data: {
  originalOrderNo: string // 原订单号(必填)
  refundAmount: number // 退款金额(必填)
  refundReason?: string // 退款原因
}): Promise<number> => {
  return request.post({ url: '/settle/payment-refund/create', data })
}

/**
 * 手动同步退款状态
 * @param id 退款ID
 * @returns 同步结果
 */
export const syncRefundStatusApi = (id: number): Promise<boolean> => {
  return request.post({ url: `/settle/payment-refund/sync-status?id=${id}` })
}

/**
 * 获取支付统计数据
 * @param params 查询参数
 * @returns 统计数据
 */
export const getPaymentStatisticsApi = (params: {
  merchantNo?: string
  paymentMethod?: number
  dateRange?: [string, string]
}): Promise<{
  totalAmount: number // 总交易金额
  totalCount: number // 总交易笔数
  successAmount: number // 成功交易金额
  successCount: number // 成功交易笔数
  refundAmount: number // 退款金额
  refundCount: number // 退款笔数
  successRate: number // 成功率(%)
}> => {
  return request.get({ url: '/settle/payment-order/statistics', params })
}

/**
 * 导出支付订单数据
 * @param params 查询参数
 * @returns 导出文件
 */
export const exportPaymentOrderApi = (params: PaymentOrderPageReqVO): Promise<void> => {
  return request.download({
    url: '/settle/payment-order/export',
    params
  })
}

/**
 * 导出退款记录数据
 * @param params 查询参数
 * @returns 导出文件
 */
export const exportPaymentRefundApi = (params: {
  refundNo?: string
  originalOrderNo?: string
  merchantNo?: string
  refundStatus?: string
  createTime?: [string, string]
}): Promise<void> => {
  return request.download({
    url: '/settle/payment-refund/export',
    params
  })
}
